package com.xq.jltx.rxjavaretrofitdemopro.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xq.jltx.rxjavaretrofitdemopro.R;
import com.xq.jltx.rxjavaretrofitdemopro.utils.ReflectUtil;
import com.xq.jltx.rxjavaretrofitdemopro.utils.XqLog;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.recyclerview.CommonAdapter;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/08/16.
 */
public class BaseFragment extends Fragment{
    @Bind(R.id.recycler)
    RecyclerView recycler;

    @Bind(R.id.result)
    TextView result;

    @Bind(R.id.progressBar)
    ProgressBar progressBar;


    CompositeSubscription mCompositeSubscription;

    public void addSubscription(Subscription s) {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = new CompositeSubscription();
        }

        this.mCompositeSubscription.add(s);
    }

    public void unsubscribe() {
        if (this.mCompositeSubscription != null) {
            this.mCompositeSubscription.clear();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.base_fragment_layout,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        ButterKnife.bind(getActivity());
        ActionBar supportActionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
        if(null != supportActionBar){
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    private void initView(){
        final List<Method> methodList = getMethods();
        CommonAdapter commonAdapter = new CommonAdapter<Method>(getContext(), R.layout.recyler_view_item, methodList) {
            @Override
            public void convert(ViewHolder holder, Method m) {
                holder.setText(R.id.text, m.getName());
            }
        };

        commonAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, View view, Object o, int position) {
                ReflectUtil.invokeMethod(BaseFragment.this,
                        methodList.get(position).getName());
            }


            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
                return false;
            }
        });

        recycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        recycler.setAdapter(commonAdapter);

    }
    @OnClick(R.id.clear)
    public void clear() {
        result.setText("");
    }


    @OnClick(R.id.unsubscribe)
    public void unsubscribeClick() {
        unsubscribe();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            getActivity().onBackPressed();
            return true;
        }
        else {
            return super.onOptionsItemSelected(item);
        }
    }

    private List<Method> getMethods() {
        List<Method> result = new ArrayList<>();
        Method[] reflects = ReflectUtil.getAllDeclaredMethod(this.getClass());
        for (Method method : reflects) {
            if (!method.getName().contains("$") && Modifier.isPublic(method.getModifiers())) {
                result.add(method);
            }
        }
        Collections.sort(result, new Comparator<Method>() {
            @Override
            public int compare(Method lhs, Method rhs) {
                return lhs.getName().compareTo(rhs.getName());
            }
        });
        return result;
    }

    public void progressVisible() {
        if (null != progressBar)
            progressBar.setVisibility(View.VISIBLE);
    }

    public void progressGone() {
        if (null != progressBar)
            progressBar.setVisibility(View.GONE);
    }

    public void logger(Object s) {
        XqLog.d(String.valueOf(s));
        result.setText(result.getText() + "\n" + s);
//        Observable.just(s).observeOn(AndroidSchedulers.mainThread()).subscribe(i -> {
//            result.setText(result.getText() + "\n" + i);
//        });

    }

    public void loggerErro(Throwable e){
        XqLog.e("出现异常");
    }

}
